﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ECCSystem
{
    public partial class frmEntryAdd : Form
    {
        int intBusinessID = BizRule.ClsCommon.gintBusinessID;
        float fltOperationTime = 0.0f;
        float fltPauseTime = 0.0f;
        int intHoldCount = 0;
        DateTime dtOperationStart = new DateTime();
        DateTime dtPauseStart = new DateTime();
        TimeSpan tsTemp = new TimeSpan();
        BizRule.ClsEntry objBREntry;
        BizRule.ClsMailCategory objBRMC;
        BizRule.ClsSaleCommodity objBRSC;

        public frmEntryAdd()
        {
            InitializeComponent();
        }

        //Load event
        private void frmEntryAdd_Load(object sender, EventArgs e)
        {
            try
            {
                //set gintBusinessID as default
                BizRule.ClsCommon.gintBusinessID = 0;
                this.BindMailCategory();
                this.BindSaleCommodity();
                if (intBusinessID != 0)
                {
                    //bind Hold Data
                    this.BindHoldStatusData();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "ECC Catch Error");
            }
        }

        //unload event
        private void frmEntryAdd_FormClosing(object sender, FormClosingEventArgs e)
        {
            Form frmEntryMains = new frmEntryMain();
            frmEntryMains.Show();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            dtOperationStart = DateTime.Now;
            btnStart.Enabled = false;
            btnPause.Enabled = true;
            btnHold.Enabled = true;
            btnSave.Enabled = true;
            cboMailCategory.Enabled = true;
            txtMailSubject.Enabled = true;
            clbSaleCommodity.Enabled = true;
        }

        private void btnPause_Click(object sender, EventArgs e)
        {

            if (btnPause.Text == "暫停")
            {
                tsTemp = DateTime.Now.Subtract(dtOperationStart);
                fltOperationTime = fltOperationTime + Convert.ToInt64(tsTemp.TotalSeconds); 
                dtPauseStart = DateTime.Now;
                btnPause.Text = "続き";
                btnPause.Enabled = true;
                btnHold.Enabled = false;
                btnSave.Enabled = false;
                cboMailCategory.Enabled = false;
                txtMailSubject.Enabled = false;
                clbSaleCommodity.Enabled = false;
            }
            else
            {
                tsTemp = DateTime.Now.Subtract(dtPauseStart);
                fltPauseTime = fltPauseTime + Convert.ToInt64(tsTemp.TotalSeconds);
                dtOperationStart = DateTime.Now;
                btnPause.Text = "暫停";
                btnPause.Enabled = true;
                btnHold.Enabled = true;
                btnSave.Enabled = true;
                cboMailCategory.Enabled = true;
                txtMailSubject.Enabled = true;
                clbSaleCommodity.Enabled = true;
            }

        }

        private void btnHold_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.CheckForm() == true)
                {
                    StringBuilder strSaleCommodityID = new StringBuilder();
                    DataRowView objDRV;

                    intHoldCount = intHoldCount + 1;
                    tsTemp = DateTime.Now.Subtract(dtOperationStart);
                    fltOperationTime = fltOperationTime + Convert.ToInt64(tsTemp.TotalSeconds);
                    //convert seconds into munites
                    fltOperationTime = fltOperationTime / 60;
                    fltPauseTime = fltPauseTime / 60;

                    //Get SaleCommodityID's strings
                    for (int i = 0; i < clbSaleCommodity.CheckedItems.Count; i++)
                    {
                        objDRV = (DataRowView)clbSaleCommodity.CheckedItems[i];
                        if (strSaleCommodityID.ToString().Trim().Length > 0)
                        {
                            strSaleCommodityID.Append(",");
                        }

                        strSaleCommodityID.Append(objDRV.Row.ItemArray[0].ToString());
                    }
                    objBREntry = new ECCSystem.BizRule.ClsEntry();
                    if (intBusinessID != 0)
                    {
                        //edit hold status business data
                        objBREntry.EditHoldEntry(Convert.ToInt32(cboMailCategory.SelectedValue.ToString()), txtMailSubject.Text.ToString().Trim(),
                            strSaleCommodityID.ToString(), fltOperationTime, fltPauseTime, intHoldCount, "保留", intBusinessID);
                        this.Close();
                    }
                    else
                    {
                        //Add new business data
                        objBREntry.AddEntry(Convert.ToInt32(cboMailCategory.SelectedValue.ToString()), txtMailSubject.Text.ToString().Trim(),
                            strSaleCommodityID.ToString(), fltOperationTime, fltPauseTime, intHoldCount, "保留");
                        this.Close();

                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "ECC Catch Error");
            }

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (CheckForm() == true)
                {
                    StringBuilder strSaleCommodityID = new StringBuilder();
                    DataRowView objDRV;

                    tsTemp = DateTime.Now.Subtract(dtOperationStart);
                    fltOperationTime = fltOperationTime + Convert.ToInt64(tsTemp.TotalSeconds);
                    //convert seconds into munites
                    fltOperationTime = fltOperationTime / 60;
                    fltPauseTime = fltPauseTime / 60;

                    //Get SaleCommodityID's strings
                    for (int i = 0; i < clbSaleCommodity.CheckedItems.Count; i++)
                    {
                        objDRV = (DataRowView)clbSaleCommodity.CheckedItems[i];
                        if (strSaleCommodityID.ToString().Trim().Length > 0)
                        {
                            strSaleCommodityID.Append(",");
                        }

                        strSaleCommodityID.Append(objDRV.Row.ItemArray[0].ToString());
                    }
                    objBREntry = new ECCSystem.BizRule.ClsEntry();
                    if (intBusinessID != 0)
                    {
                        //edit hold status business data
                        objBREntry.EditHoldEntry(Convert.ToInt32(cboMailCategory.SelectedValue.ToString()), txtMailSubject.Text.ToString().Trim(),
                            strSaleCommodityID.ToString(), fltOperationTime, fltPauseTime, intHoldCount, "終了", intBusinessID);
                        System.GC.Collect();
                        this.Close();
                    }
                    else
                    {
                        //Add new business data
                        objBREntry.AddEntry(Convert.ToInt32(cboMailCategory.SelectedValue.ToString()), txtMailSubject.Text.ToString().Trim(),
                            strSaleCommodityID.ToString(), fltOperationTime, fltPauseTime, intHoldCount, "終了");
                        System.GC.Collect();
                        this.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "ECC Catch Error");
            }
        }

        //bind hold status's data in controls
        private void BindHoldStatusData()
        {
            string strSaleCommodityID = string.Empty;
            string[] arSaleCommodityID;
            DataRowView objDRVData;
            DataSet objDSHoldData = new DataSet();
            objBREntry = new ECCSystem.BizRule.ClsEntry();
            objDSHoldData = (DataSet)objBREntry.GetEntry(intBusinessID);
            cboMailCategory.SelectedValue = objDSHoldData.Tables[0].Rows[0][1];
            txtMailSubject.Text = objDSHoldData.Tables[0].Rows[0][2].ToString();
            strSaleCommodityID = objDSHoldData.Tables[0].Rows[0][3].ToString();
            arSaleCommodityID=strSaleCommodityID.Split(',');
            //Set checked items as Check
            for (int i = 0; i < clbSaleCommodity.Items.Count; i++)
            {
                objDRVData = (DataRowView)clbSaleCommodity.Items[i];
                for (int j = 0; j < arSaleCommodityID.Length; j++)
                {
                    if (arSaleCommodityID[j] == objDRVData.Row.ItemArray[0].ToString())
                    {
                        clbSaleCommodity.SetItemChecked(i, true);
                    } 
                }
            }
            //convert minutes into seconds
            fltOperationTime = float.Parse(objDSHoldData.Tables[0].Rows[0][4].ToString())*60;
            fltPauseTime = float.Parse(objDSHoldData.Tables[0].Rows[0][5].ToString())*60;
            intHoldCount = Convert.ToInt32(objDSHoldData.Tables[0].Rows[0][6].ToString());
        }

        //bind mailcategory data into cboMailCategory
        private void BindMailCategory()
        {
            DataSet objDSMC = new DataSet();
            objBRMC = new ECCSystem.BizRule.ClsMailCategory();
            objDSMC=(DataSet)objBRMC.GetList();
            objDSMC.Tables[0].Rows.Add("0", "");
            objDSMC.Tables[0].DefaultView.Sort = "ID";
            cboMailCategory.DataSource = objDSMC.Tables[0];
            cboMailCategory.DisplayMember = "MailCategory";
            cboMailCategory.ValueMember = "ID";
 
        }

        //bind sale commodity data into clbSaleCommodity
        private void BindSaleCommodity()
        {
            DataSet objDSSC = new DataSet();
            objBRSC = new ECCSystem.BizRule.ClsSaleCommodity();
            objDSSC = (DataSet)objBRSC.GetList();
            objDSSC.Tables[0].DefaultView.Sort = "ID";
            clbSaleCommodity.DataSource = objDSSC.Tables[0];
            clbSaleCommodity.DisplayMember = "SaleCommodity";
            clbSaleCommodity.ValueMember = "ID";
        }

        //Check data validation
        private bool CheckForm()
        {
            if (cboMailCategory.Text.ToString().Trim().Length == 0)
            {
                MessageBox.Show("カテゴリを入力してください！", "ECC Information");
                cboMailCategory.Focus();
                return false;
            }
            if (txtMailSubject.Text.Trim().Length == 0)
            {
                MessageBox.Show("件名を入力してください！", "ECC Information");
                txtMailSubject.Focus();
                return false;
            }
            return true;
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("データを保存しなくても宜しいですか？", "ECC Information", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                this.Close();
            }
        }
    }
}